package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.*;
import com.zw.stock_sell_storage.mapper.PurchasingCarMapper;
import com.zw.stock_sell_storage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class PurchasingCarServiceImpl extends ServiceImpl<PurchasingCarMapper, PurchasingCarDto> implements IPurchasingCarService {
    @Autowired
    PurchasingCarMapper purchasingCarMapper;

    @Autowired
    IInventoryService iInventoryService;

    @Autowired
    IPurchasingInformationService iPurchasingInformationService;

    @Autowired
    IPurchasingDetailService iPurchasingDetailService;
    @Autowired
    ILogService iLogService;


    @Override
    public List<PurchasingCarDto> queryPurchasingCarList(int userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("fk_user_id",userId);
        return purchasingCarMapper.selectList(queryWrapper);
    }

    @Override
    public PurchasingCarDto queryPurchasingCarByGoodsCode(String GoodsCode,int userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("fk_user_id",userId);
        queryWrapper.eq("goods_code",GoodsCode);
        return purchasingCarMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public boolean addPurchasingCar(PurchasingCarDto purchasingCarDto) {
        PurchasingCarDto purchasingCarDto1 = queryPurchasingCarByGoodsCode(purchasingCarDto.getGoodsCode(), purchasingCarDto.getFkUserId());
        //购物车是否存在
        if (purchasingCarDto1!=null){
            purchasingCarDto1.setGoodsPurchasingNumber(purchasingCarDto.getGoodsPurchasingNumber()+purchasingCarDto1.getGoodsPurchasingNumber());
            updatePurchasingCar(purchasingCarDto1);
        }
        else{
            InventoryDto inventoryDto = iInventoryService.queryInventoryByGoodsCode(purchasingCarDto.getGoodsCode());
            //库存是否存在
            if (inventoryDto==null){
                inventoryDto = new InventoryDto();
                inventoryDto.setGoodsCode(purchasingCarDto.getGoodsCode());
                inventoryDto.setGoodsName(purchasingCarDto.getGoodsName());
                inventoryDto.setGoodsBrand(purchasingCarDto.getGoodsBrand());
                inventoryDto.setGoodsFace(purchasingCarDto.getGoodsFace());
                inventoryDto.setGoodsType(purchasingCarDto.getGoodsType());
                inventoryDto.setGoodsNumber(0);
                inventoryDto.setGoodsNewPurchasingPrice(0.00);
                inventoryDto.setGoodsSalePrice(0.00);
                iInventoryService.addInventory(inventoryDto);
            }
            purchasingCarMapper.insert(purchasingCarDto);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updatePurchasingCar(PurchasingCarDto purchasingCarDto) {

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("purchasing_car_id",purchasingCarDto.getPurchasingCarId());
        purchasingCarMapper.update(purchasingCarDto, updateWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean rmvPurchasingCarById(int purchasingCarId) {

        purchasingCarMapper.deleteById(purchasingCarId);
        return true;
    }

    @Override
    @Transactional
    public boolean rmvAllPurchasingCar(int userId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("fk_user_id",userId);
        purchasingCarMapper.delete(queryWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean pushPurchasingCar(PurchasingInformationDto purchasingInformationDto,int userId) {

        List<PurchasingCarDto> purchasingCarList = queryPurchasingCarList(userId);
        if (purchasingCarList==null){
            return false;
        }
        //添加采购记录
        iPurchasingInformationService.addPurchasingInformation(purchasingInformationDto);
        //采购明细集合
        List<PurchasingDetailDto> purchasingDetailList = new ArrayList<>();
        for (PurchasingCarDto purchasingCarDto : purchasingCarList) {
            //更新库存
            InventoryDto inventoryDto = iInventoryService.queryInventoryByGoodsCode(purchasingCarDto.getGoodsCode());
            if (inventoryDto!=null) {
                inventoryDto.setGoodsNumber(inventoryDto.getGoodsNumber()+purchasingCarDto.getGoodsPurchasingNumber());
                inventoryDto.setGoodsNewPurchasingPrice(purchasingCarDto.getGoodsPurchasingPrice());
                iInventoryService.updateInventory(inventoryDto);

            }
            else{
                inventoryDto = new InventoryDto();
                inventoryDto.setGoodsCode(purchasingCarDto.getGoodsCode());
                inventoryDto.setGoodsName(purchasingCarDto.getGoodsName());
                inventoryDto.setGoodsBrand(purchasingCarDto.getGoodsBrand());
                inventoryDto.setGoodsType(purchasingCarDto.getGoodsType());
                inventoryDto.setGoodsFace(purchasingCarDto.getGoodsFace());
                inventoryDto.setGoodsNumber(purchasingCarDto.getGoodsPurchasingNumber());
                inventoryDto.setGoodsNewPurchasingPrice(purchasingCarDto.getGoodsPurchasingPrice());
                inventoryDto.setGoodsSalePrice(0.00);
                iInventoryService.addInventory(inventoryDto);
            }
            //添加采购明细集合
            PurchasingDetailDto purchasingDetailDto = new PurchasingDetailDto();
            purchasingDetailDto.setPurchasingDetailGoodsName(purchasingCarDto.getGoodsName());
            purchasingDetailDto.setPurchasingDetailGoodsBrand(purchasingCarDto.getGoodsBrand());
            purchasingDetailDto.setPurchasingDetailPrice(purchasingCarDto.getGoodsPurchasingPrice());
            purchasingDetailDto.setPurchasingDetailNumber(purchasingCarDto.getGoodsPurchasingNumber());
            purchasingDetailDto.setFkPurchasingId(purchasingInformationDto.getPurchasingId());
            purchasingDetailList.add(purchasingDetailDto);
        }
        //添加采购明细
        iPurchasingDetailService.addPurchasingDetails(purchasingDetailList);
        //清空购物车
        rmvAllPurchasingCar(userId);
        LogDto logDto = new LogDto();
        logDto.setLogDate(purchasingInformationDto.getPurchasingTime());
        logDto.setLogDesc(purchasingInformationDto.getPurchasingAgent()+"于"+purchasingInformationDto.getPurchasingTime()+"入库商品，采购信息id为："+purchasingInformationDto.getPurchasingId());
        iLogService.addLog(logDto);
        return true;
    }
}
